import 'package:erp/data/model/process.dart';
import 'package:erp/res/colors.dart';
import 'package:erp/widgets/load_image.dart';
import 'package:erp/widgets/my_button.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

class FlowTodoCard extends StatelessWidget {
  final Function? onEdit;
  final Function? onDetail;
  final Function? onHandle;
  final Function? onDelete;
  final Process process;
  late final String empName;
  late final String title;
  final bool readOnly;

  final Map<int, Widget> processStatus2Pic = {
    0: LoadAssetImage(
      'flow/status_uncommitted',
      width: 105.w,
      height: 100.h,
    ),
    1: LoadAssetImage(
      'flow/status_review',
      width: 105.w,
      height: 100.h,
    ),
    2: LoadAssetImage(
      'flow/status_back',
      width: 105.w,
      height: 100.h,
    ),
    3: LoadAssetImage(
      'flow/status_completed',
      width: 105.w,
      height: 100.h,
    ),
  };

  FlowTodoCard({
    Key? key,
    this.onEdit,
    this.onDetail,
    this.onHandle,
    this.onDelete,
    this.readOnly = false,
    required this.process,
  }) {
    RegExp exp = new RegExp(r"^【(.*)】.*");
    var matches = exp.allMatches(process.processName);
    var match = matches.elementAt(0);
    this.empName = match.group(1) ?? "";
    this.title = process.processName.replaceAll(RegExp(r"【.*】"), "");
  }

  Widget _generateButton({String? text, bool isDelete = false, Function? tapFunc}) {
    return Container(
      margin: EdgeInsets.only(left: 10.w),
      width: 100.w,
      height: 40.h,
      decoration: BoxDecoration(
        color: isDelete ? Colors.white : Color(0xff4C88FF),
        borderRadius: BorderRadius.all(Radius.circular(4.w)),
        border: Border.all(color: Color(0xff4C88FF)),
      ),
      child: MyButton(
        text: "${text}",
        onPressed: () {
          if (tapFunc != null) {
            tapFunc();
          }
        },
        textColor: isDelete ? Color(0xff666666) : Colors.white,
        backgroundColor: Colors.transparent,
        fontSize: 20.sp,
        padding: EdgeInsets.zero,
        side: BorderSide.none,
      ),
    );
  }

  List<Widget> _buildButtonBox(int status, readOnly) {
    if (readOnly) {
      return [
        _generateButton(
          text: "详情",
          tapFunc: () {
            if (onDetail != null) {
              onDetail!();
            }
          },
        ),
      ];
    }
    if (status == 0 || status == 2) {
      return [
        _generateButton(
          text: "修改提交",
          tapFunc: () {
            if (onEdit != null) {
              onEdit!();
            }
          },
        ),
        _generateButton(
          text: "详情",
          tapFunc: () {
            if (onDetail != null) {
              onDetail!();
            }
          },
        ),
        _generateButton(
          text: "删除",
          isDelete: true,
          tapFunc: () {
            if (onDelete != null) {
              onDelete!();
            }
          },
        ),
      ];
    } else {
      return [
        _generateButton(
          text: "办理",
          tapFunc: () {
            if (onHandle != null) {
              onHandle!();
            }
          },
        ),
        _generateButton(
          text: "详情",
          tapFunc: () {
            if (onDetail != null) {
              onDetail!();
            }
          },
        ),
      ];
    }
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: EdgeInsets.symmetric(horizontal: 32.w),
      margin: EdgeInsets.only(bottom: 20.w),
      width: double.infinity,
      child: Stack(
        children: [
          Row(
            children: [
              Container(
                width: 204.w,
                height: 260.h,
                decoration: BoxDecoration(
                  borderRadius: BorderRadius.only(
                    topLeft: Radius.circular(10.w),
                    topRight: Radius.circular(0),
                    bottomRight: Radius.circular(0),
                    bottomLeft: Radius.circular(10.w),
                  ),
                  color: Color(0xFF4C88FF),
                ),
                child: Container(
                  padding: EdgeInsets.symmetric(horizontal: 45.w, vertical: 41.w),
                  child: Center(
                    child: Tooltip(
                      message: '${title}',
                      child: Text(
                        "${title}",
                        textAlign: TextAlign.center,
                        style: TextStyle(color: Colours.white, fontWeight: FontWeight.w400, fontSize: 28.sp),
                        overflow: TextOverflow.ellipsis,
                        maxLines: 3,
                      ),
                    ),
                  ),
                ),
              ),
              SizedBox(width: 12.w),
              Container(
                width: 470.w,
                height: 260.h,
                decoration: BoxDecoration(
                  borderRadius: BorderRadius.only(
                    topLeft: Radius.circular(0),
                    topRight: Radius.circular(10.w),
                    bottomRight: Radius.circular(10.w),
                    bottomLeft: Radius.circular(0),
                  ),
                  color: Colours.white,
                ),
                child: Stack(
                  children: [
                    Positioned(
                      left: 30.w,
                      top: 37.w,
                      child: Container(
                        width: 400.w,
                        height: 123.h,
                        child: Column(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: [
                            Text(
                              "流程编号：${process.processNo}",
                              style: TextStyle(fontSize: 24.sp),
                            ),
                            Text(
                              "申请人：${empName}",
                              style: TextStyle(fontSize: 24.sp),
                            ),
                            Text(
                              "申请时间：${process.processDate}",
                              style: TextStyle(fontSize: 24.sp),
                            ),
                          ],
                        ),
                      ),
                    ),
                    Positioned(
                      top: 10.w,
                      right: 10.w,
                      child: processStatus2Pic[process.processStatus] ?? SizedBox.shrink(),
                    ),
                    Positioned(
                      left: 25.w,
                      right: 22.w,
                      bottom: 64.h,
                      child: LoadAssetImage(
                        'flow/dash_border',
                        width: 423.w,
                        height: 1.h,
                      ),
                    ),
                    Positioned(
                      bottom: 0.w,
                      left: 25.w,
                      right: 22.w,
                      child: ConstrainedBox(
                        constraints: BoxConstraints(maxHeight: 64.h, minWidth: 422.w),
                        child: Padding(
                          padding: EdgeInsets.symmetric(vertical: 12.w),
                          child: Row(
                            mainAxisAlignment: MainAxisAlignment.end,
                            children: _buildButtonBox(process.processStatus, readOnly),
                          ),
                        ),
                      ),
                    ),
                  ],
                ),
              )
            ],
          ),
          Positioned(
            left: 186.w,
            top: 57.w,
            child: Container(
              height: 130.h,
              child: Column(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  LoadAssetImage(
                    'flow/link_ring',
                    width: 47.w,
                    height: 11.h,
                  ),
                  LoadAssetImage(
                    'flow/link_ring',
                    width: 47.w,
                    height: 11.h,
                  )
                ],
              ),
            ),
          ),
        ],
      ),
    );
  }
}
